angular.module('usercenter.auth', [

])

    .config(['$stateProvider',   '$urlRouterProvider',
        function($stateProvider, $urlRouterProvider) {
            $stateProvider
                .state('regedit', {
                    url : '/regedit',
                    templateUrl : usercenter_templateprefix+'auth/regedit.html',
                    resolve:{
                        login: ['$state','usercenter_AuthService',function($state,usercenter_AuthService){
                            if(usercenter_AuthService.getUserid()!=''&&usercenter_AuthService.getUserid()!=undefined){//有id则表示已经登录过。
                                window.location.href = projectBaseUrl+'user';
                            }
                        }]
                    },
                    controller : 'RegeditController'
                })
                .state('modify', {
                    url : '/modify',
                    templateUrl : usercenter_templateprefix+'auth/modifypsw.html',
                    controller : 'ModifyController'
                })
                .state('login', {
                    url : '/login',
                    templateUrl : usercenter_templateprefix+'auth/login.html',
                    resolve:{
                        login: ['$state','usercenter_AuthService',function($state,usercenter_AuthService){
                            if(usercenter_AuthService.getUserid()!=''&&usercenter_AuthService.getUserid()!=undefined){//有id则表示已经登录过。
                                return true;
                            }else{
                                return false;
                            }
                        }]
                    },
                    controller : 'LoginController'
                })
        }])
    //注册页面控制器
    .controller('RegeditController', ['$scope', '$state', 'usercenter_AuthService', function($scope, $state, usercenter_AuthService){
        $scope.customer = "";
        $scope.getcode = function(phone){
            var ph = $(".tel_ipt").val();
            //var nam = $(".name").val();
            //var fist = nam.substring(1,0)
            //var pas = $(".psw").val()
            var flag = false;
            var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;
            if(ph == ''){
                input_alert("手机号码不能为空！");
                $(".tel_ipt").focus()
            }else if(ph.length !=11){
                input_alert("请输入有效的手机号码！");
                $(".tel_ipt").focus()
                $(".tel_ipt").select()
            }else if(!myreg.test(ph)){
                input_alert( "请输入有效的手机号码！");
                $(".tel_ipt").focus()
                $(".tel_ipt").select()
            }else{
                usercenter_AuthService.getcode(phone);
                settime();
            }
        }
        $scope.regedit = function(){
            $scope.success_func = function(){
                alert("注册成功");
                $state.go('login');
            }
            $scope.error_func = function(){
                alert("注册失败");
            }
            usercenter_AuthService.register($scope.customer,$scope.success_func,$scope.error_func);
        }
    }])

    .controller('ModifyController', ['$scope',function($scope){

    }])

    .controller('LoginController', ['$scope', '$state','login', 'usercenter_AuthService', function($scope, $state,login,usercenter_AuthService){
        $scope.ent_mallurl = projectBaseUrl;
        $scope.user = "";
        if(login){//有id则表示已经登录过。
            $state.go("ucmodel.user");
        }
        $scope.login = function(){
            $scope.success_func = function(){//登录成功回调函数
                /*$state.go("ucmodel.user");*/
                window.location.href =history.back();
            }
            $scope.error_func = function(data){//登录失败回调函数
                input_alert(data.data.reason);
            }
            usercenter_AuthService.login($scope.user,$scope.success_func,$scope.error_func);
        }
    }])